package com.weacar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.weacar.R;
import com.weacar.bean.AuctionBean;
import com.weacar.utils.PriceUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AuctionSuccessAdapter extends BaseAdapter {
    private List<AuctionBean.MsgBean> mDatas;
    private Context mContext;

    private LayoutInflater mInflater;

    public AuctionSuccessAdapter(List<AuctionBean.MsgBean> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        AuctionBean.MsgBean msgBean = mDatas.get(i);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_auction_success, viewGroup, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_title.setText(msgBean.getName());
        holder.tv_condition.setText(msgBean.getLicensing_date() + "|" + msgBean.getCapacity_name() + "|" + String.format("%.2f", msgBean.getDisplayed_mileage()) + "万公里");
        if (msgBean.getOffer_price().contains(".")) {
            holder.tv_my_price.setText("我的出价:" + PriceUtil.getPriceByWan(Integer.parseInt(msgBean.getOffer_price().substring(0, msgBean.getOffer_price().indexOf(".")))) + "万" + "(中标)");
        } else {
            holder.tv_my_price.setText("我的出价:" + PriceUtil.getPriceByWan(Integer.parseInt(msgBean.getOffer_price())) + "万" + "(中标)");
        }

        return convertView;
    }

    static class ViewHolder {

        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_condition)
        TextView tv_condition;
        @BindView(R.id.tv_my_price)
        TextView tv_my_price;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
